<script>
export default {
  name:"productAttribute",
  data() {
    let checkAttributeName =(rule,value,callback)=>{
      if(this.form.id){
        return callback();
      }
      this.$axios.post("/zzh-product-attribute/findeByName?name="+this.form.name).then(res=>res.data).then(res=>{
        if(res.resultCode!=200){
          callback()
        }else{
          callback(new Error('商品属性已经存在'));
        }
      })
    };
    return {
      tableData: [],
      pageNum:1,
      pageSize:5,
      total:5,
      attributeName:'',
      dialogVisible:false,
      loading: true,
      form:{
        id:'',
        attributeName:'',
        attributeValue:'',
        status:''
      },
      rules: {
        attributeName: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {validator:checkAttributeName,trigger: 'blur'}
        ],
        attributeValue: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' },
        ]
      }
    };
  },
  methods:{
    //查询所有属性信息
    loadPost(){
      this.loading = true;
      this.$axios.post('/zzh-product-attribute/list',{
        page:this.pageNum,
        limit:this.pageSize,
        name:this.attributeName
      }).then(res=>res.data).then(res=>{
        this.loading = false;
        if(res.resultCode==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }
      })
    },
    //点击添加按钮，弹出添加对话框
    add(){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    //判断点击了修改按钮还是添加按钮
    loadAdd(){
      //表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.form.id){
            this.doUpdate();
            this.form.id='';
          }else {
            this.doAdd()
          }
        } else {
          return false;
        }
      });
    },
    //进行添加操作
    doAdd(){
      this.$axios.post('/zzh-product-attribute/add',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '新增数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '新增数据失败',
            type: 'error'
          });
        }
      })
    },
    //进行更新前赋值操作
    mod(row){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.form.id=row.id
        this.form.attributeName=row.attributeName
        this.form.attributeValue=row.attributeValue
      })
    },
    //进行更新操作
    doUpdate(){
      this.$axios.put('/zzh-product-attribute/update',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '更新数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '更新数据失败',
            type: 'error'
          });
        }
      })
    },
    //进行删除操作
    del(id){
      this.$axios.delete('/zzh-product-attribute/delete?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'error'
          });
        }
      })
    },
  updateStatus(id){
    this.$axios.put('/zzh-product-attribute/updateStatus?id='+id).then(res=>res.data).then(res=>{
      if(res.resultCode==200){
        this.$message({
          showClose: true,
          message: '更新状态成功',
          type: 'success'
        });
        this.loadPost()
      }else {
        this.$message({
          showClose: true,
          message: '更新状态失败',
          type: 'error'
        });
      }
    })
  },
    //进行分页操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    //重新刷新
    resetParam(){
      this.attributeName=''
      this.loadPost()
    },
    //重置窗体表单
    resetForm() {
      //通过this.$refs.form可以访问名为"form"的子组件或DOM元素
      //调用resetFields()方法将重置表单元素的值和校验状态，使表单恢复到最初的空白状态
      this.$refs.form.resetFields();
    }
  },
  beforeMount() {
    this.loadPost();
  }
}
</script>

<template>
  <div>
    <!--    按钮-->
    <div style="margin-bottom: 5px">
      <el-input v-model="attributeName" placeholder="请输入名字" suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>

      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>
    <!--    表单-->
    <el-table :data="tableData" v-loading="loading"
              :header-cell-style="{background:'#f3f6fd',color:'#555'}">
      <el-table-column prop="id" label="id" width="140">
      </el-table-column>
      <el-table-column prop="attributeName" label="属性名" width="150">
      </el-table-column>
      <el-table-column prop="attributeValue" label="属性值" width="300">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.status === 1 ? 'success' : 'danger'"
              disable-transitions>{{scope.row.status === 1 ? '正常' : scope.row.status === 2 ? '禁用' : '已删除'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              style="margin-left: 5px;margin-right: 5px"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>
          <el-button size="small" type="success" @click="updateStatus(scope.row.id)">启用/禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--    提示框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="40%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
        <el-form-item label="属性名" prop="attributeName">
          <el-input v-model="form.attributeName" placeholder="属性名"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="attributeValue">
          <el-input v-model="form.attributeValue" placeholder="属性值"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="loadAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>